package main

import (
	"fmt"
	"strings"
)

func MakeHasPrefix(prefix string) func(string) bool {
	return func(s string) bool {
		return strings.HasPrefix(s, prefix)
	}
}

func MakeHasSuffix(suffix string) func(string) bool {
	return func(s string) bool {
		return strings.HasSuffix(s, suffix)
	}
}

func MakeIsContains(str string) func(string) bool {
	return func(sub string) bool {
		return strings.Contains(str, sub)
	}
}

func MakeStringIndex(str string) func(string) int {
	return func(s string) int {
		return strings.Index(str, s)
	}
}

func MakeStringLastIndex(str string) func(string) int {
	return func(s string) (index int) {
		return strings.LastIndex(str, s)
	}
}

func main() {
	// 判断前置
	hasPrefixGo := MakeHasPrefix("go")
	fmt.Printf("golang has prefix 'go': %v\n", hasPrefixGo("golang"))
	fmt.Printf("google has prefix 'go': %v\n", hasPrefixGo("google"))

	// 判断后置
	hasSuffixLang := MakeHasSuffix("lang")
	fmt.Printf("golang has suffix 'lang': %v\n", hasSuffixLang("golang"))
	fmt.Printf("dolang has suffix 'lang': %v\n", hasSuffixLang("dolang"))

	// 判断包含Contains
	isContainsInGolang := MakeIsContains("golang")
	fmt.Printf("golang contains g : %v\n", isContainsInGolang("g"))
	fmt.Printf("golang contains a : %v\n", isContainsInGolang("a"))

	// 判断index
	findIndexInGolang := MakeStringIndex("golang")
	fmt.Printf("the index of l in golang is : %v\n", findIndexInGolang("l"))
	fmt.Printf("the index of l in golang is : %v\n", findIndexInGolang("l"))

	// 判断last index
	findLastIndexInGoogle := MakeStringLastIndex("google")
	fmt.Printf("the last index of o in google is : %v\n", findLastIndexInGoogle("l"))
	fmt.Printf("the last index of g in google is : %v\n", findLastIndexInGoogle("o"))

	// 生成重复字符串
	fmt.Println("a repeat 10time: ", strings.Repeat("a", 10))
	// 字符串替换
	fmt.Println("golang's o replace to a: ", strings.Replace("golang", "o", "a", 0))
	// 统计字符串出现次数
	fmt.Println("count g in golanggogogo: ", strings.Count("golanggogogo", "g"))
	// 转换大小写
	fmt.Println("Upper golang : ", strings.ToUpper("golang"))
	fmt.Println("Lower golang : ", strings.ToLower("golang"))
	// 修剪字符串
	fmt.Println("trim 'g' from ' golang ': ", strings.Trim(" golang ", "g"))
	// 修剪空格
	fmt.Println("trim space from ' golang ': ", strings.TrimSpace(" golang "))
	// 分割
	study := "go home to strudy"
	fmt.Printf("fileds in '%s': %v\n", study, strings.Fields(study)) // [[go home to strudy]]
	// 拼接slice
	fmt.Println(strings.Join(strings.Fields(study), "-")) // go-home-to-strudy
}
